enchant();

function shuffle(array){
    var i = array.length;
    while(--i){
        var j = Math.floor(Math.random() * (i + 1));
        if(i == j) continue;
        var k = array[i];
        array[i] = array[j];
        array[j] = k;
    }
    return array;
}

window.onload = function() {
    var gamelevel = 0;
    var totalscore = 0;
    var combo = 0;
    var max_combo = 0;
    var score_fish = 2; 
    var score_hand = 1;
    var score_penalty = -1;
    var score_to_level = [20, 50, 100, 200, 300, 500, 1000, 2000, 3000, 5000];
    //var score_to_level = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30];
    var cats = Array(5);
    var places = Array(10);
    var place_outleft = null;
    var place_outright = null;
    var hand = null;
    var back = null;
    var iconFish = null;
    var iconScore = null;
    var iconCombo = null;
    var iconTitle = null;
    var levelLabel = null;
    var pointLabel = null;
    var fish = null;
    
    var getlevel = function(score){
        for(var i = 0, len = score_to_level.length; i < len; i++){
            if(score < score_to_level[i]){
                break;
            }
        }
        return i;
    }
    
    var changelevel = function(level){
        console.log("changelevel " + level);
        gamelevel = level;        
        //level is max
        if(gamelevel >= score_to_level.length){
            return;
        }
        for(var i = 0, len = cats.length; i < len; i++){
            cats[i].level = gamelevel;
        }
        //add score
        levelLabel.text = 'Level: ' + gamelevel;
    };
    
    var game = new Game(320, 320);
    game.preload('back.png');
    game.preload('title.gif');
    game.onload = function() {
    
        //places
        for(var i = 0, len = places.length; i < len; i++){
            places[i] = new Point(32 * i , 130);
        }
        //shuffle
        places = shuffle(places);
        
        place_outleft = new Point(-32, 130);
        place_outright = new Point(game.width, 130);
        
        //background
        back = new Sprite(320, 320);
        back.image = game.assets['back.png'];
        game.rootScene.addChild(back);
        
        //fish
        iconFish = new Sprite(32, 32);
        iconFish.image = game.assets['cat.gif'];
        iconFish.frame = 18;
        iconFish.x = game.width / 2.0 - iconFish.width / 2.0;
        iconFish.y = game.height - iconFish.height;
        game.rootScene.addChild(iconFish);
        
        //fish draggable
        fish = new Fish();
        game.rootScene.addChild(fish);

        //score label
        pointLabel = new Label();
        pointLabel.x = 10;
        pointLabel.y = 10;
        pointLabel.text = 'Score: ' + totalscore;
        pointLabel.font = "0.8em 'Arial'";
        game.rootScene.addChild(pointLabel);
        
        //level label
        levelLabel = new Label();
        levelLabel.x = 100;
        levelLabel.y = 10;
        levelLabel.text = 'Level: ' + gamelevel;
        levelLabel.font = "0.8em 'Arial'";
        game.rootScene.addChild(levelLabel);
        
        //cats
        for(var i = 0, len = cats.length; i < len; i++){
            var cat = new Cat();
            var start = places.shift();
            cat.x = start.x;
            cat.y = start.y;            
            places.push(start);
            
            var place = places.shift();
            cat.goal = place;
            places.push(place);
            
            cat.level = gamelevel;
            
            game.rootScene.addChild(cat);
            cats[i] = cat;
        }

        //hand
        hand = new Hand();
        game.rootScene.addChild(hand);        

        //score
        iconScore = new Score();
        game.rootScene.addChild(iconScore);
        
        iconCombo = new Combo();
        game.rootScene.addChild(iconCombo);        
        
        iconTitle = new Sprite(320, 320);
        iconTitle.image = game.assets['title.gif'];
        game.rootScene.addChild(iconTitle);
        
        
        var enterframeHandler = function(e){
            console.log("enterframe");
            if(game.rootScene == game.currentScene){
                game.rootScene.removeChild(iconTitle);
                game.removeEventListener('enterframe', enterframeHandler);
            }
        }
        game.addEventListener('enterframe', enterframeHandler);
        
        //event handler
        game.addEventListener('mousedown', function(e) {
            if(e.dispatcher instanceof Cat){
                var cat = e.dispatcher;
                switch(cat.state){
                    case enchant.strokeCats.State.WALK:
                    case enchant.strokeCats.State.WANT:
                    //run away
                    cat.state = enchant.strokeCats.State.RUN;
                    if(cat.dx < 0) cat.goal = place_outleft;
                    else           cat.goal = place_outright;
                    
                    //combo cancel
                    combo = 0;
                    
                    //paint score
                    iconScore.paint(cat.x, cat.y, score_penalty);
                    
                    //add score
                    totalscore += score_penalty;
                    pointLabel.text = 'Score: ' + totalscore;
                    //get level
                    var level = getlevel(totalscore);
                    if(gamelevel != level){
                        changelevel(level);
                    }
                    break;
                    
                    case enchant.strokeCats.State.STOP:
                    //stroke cat
                    cat.state = enchant.strokeCats.State.SLEEP;
                    
                    var score = score_hand + Math.floor(combo / 10);
                    if(score > 9){
                        score = 9;
                    }
                    
                    //paint score
                    iconScore.paint(cat.x, cat.y, score);

                    //add score
                    totalscore += score;
                    pointLabel.text = 'Score: ' + totalscore;

                    //get level
                    var level = getlevel(totalscore);
                        if(gamelevel != level){
                            changelevel(level);
                    }

                    //score
                    combo++;
                    if(combo > max_combo) max_combo = combo;                               
                    if(combo > 1){
                        iconCombo.paint(game.width / 2.0 - 32, 50, combo);
                    }
                    break;
                }
                hand.paint(cat.x, cat.y);
            }
            else if(e.dispatcher instanceof Fish){
                var _fish = e.dispatcher;
                for(var i = 0, len = cats.length; i < len; i++){
                    if(cats[i] instanceof Cat){
                        var cat = cats[i];
                        if(cat.state == enchant.strokeCats.State.WANT){
                        
                            //collision detection radian 10
                            if(cat.intersect(_fish) ){
                                cat.state = enchant.strokeCats.State.EAT;
                                _fish.reset();

                                var score = score_fish + Math.floor(combo / 10);
                                if(score > 9){
                                    score = 9;
                                }                                
                                //paint score
                                iconScore.paint(cat.x, cat.y, score);

                                //add score
                                totalscore += score;
                                pointLabel.text = 'Score: ' + totalscore;
                                
                                //get level
                                var level = getlevel(totalscore);
                                if(gamelevel != level){
                                    changelevel(level);
                                }

                                //score
                                combo++;
                                if(combo > max_combo) max_combo = combo;                               
                                if(combo > 1){
                                    iconCombo.paint(game.width / 2.0 - 32, 50, combo);
                                }
                                break;
                            }
                        }
                    }
                }
            }
        });
        game.addEventListener('change', function(e){
            console.log("change");
            if(e.dispatcher instanceof Cat){
                var cat = e.dispatcher;
                switch(cat.state){
                    case enchant.strokeCats.State.RUN:
                    console.log("run");
                    if(cat.dx < 0) cat.goal = place_outleft;
                    else           cat.goal = place_outright;

                    //combo cancel
                    combo = 0;

                    //paint score
                    iconScore.paint(cat.x, cat.y, score_penalty);
                    
                    //add score
                    totalscore += score_penalty;
                    pointLabel.text = 'Score: ' + totalscore;
                    break;
                    
                    //get level
                    var level = getlevel(totalscore);
                    if(gamelevel != level){
                        changelevel(level);
                    }
                                                    
                    case enchant.strokeCats.State.AWAY:
                    console.log(enchant.strokeCats.State.AWAY);
                    
                    //combo cancel
                    combo = 0;
                    
                    //check gameover
                    var i = 0;
                    for(len = cats.length; i < len; i++){
                        if(cats[i].state != enchant.strokeCats.State.AWAY){
                            break;
                        }
                    }
                    if(i == len){
                        console.log("gameover");
                        //tweet message
                        var message = 
                        'あなたのとくてんは ' + totalscore + ' てんでした。'
                        + 'レベルは ' + gamelevel + ' でした。'
                        + 'さいだいコンボすうは ' + max_combo + ' 回でした。';
                        console.log(message);
                        game.end(totalscore, message); 
                    } 
                    break;
                    
                    case enchant.strokeCats.State.WALK:
                    console.log("walk");
                    var place = places.shift();
                    cat.goal = place;
                    places.push(place);
                    break;
                }
            }
        });        
        
    }
    game.start();
}
